What is point-in-polygon?
The point-in-polygon npm package is a utility for determining whether a given point lies inside a polygon. This can be useful in various applications such as geographical mapping, collision detection in games, and more.
What are point-in-polygon's main functionalities?
Check if a point is inside a polygon
This feature allows you to check if a given point is inside a specified polygon. The function returns true if the point is inside the polygon and false otherwise.
const inside = require('point-in-polygon');
const point = [1, 1];
const polygon = [[0, 0], [2, 0], [2, 2], [0, 2]];
console.log(inside(point, polygon)); // true
Other packages similar to point-in-polygon
point-in-polygon-hao
The point-in-polygon-hao package provides similar functionality to point-in-polygon, allowing you to check if a point is inside a polygon. It is a lightweight and efficient alternative.
robust-point-in-polygon
The robust-point-in-polygon package offers a more robust solution for checking if a point is inside a polygon. It handles edge cases and numerical precision issues better than point-in-polygon.
turf-inside
The turf-inside module is part of the Turf.js library, which provides advanced geospatial analysis tools. It offers more comprehensive geospatial functionalities compared to point-in-polygon, including point-in-polygon checks.
point-in-polygon
Determine if a point is inside of a polygon.
This module casts a semi-infinite ray from the inquiry point and counts intersections,
based on
this algorithm.
If you need a numerically robust solution and are willing to trade some performance for it,
use robust-point-in-polygon.
example
var pointInPolygon = require('point-in-polygon');
var polygon = [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ];
console.log(pointInPolygon([ 1.5, 1.5 ], polygon));
console.log(pointInPolygon([ 4.9, 1.2 ], polygon));
console.log(pointInPolygon([ 1.8, 1.1 ], polygon));
methods
var pointInPolygon = require('point-in-polygon')
var pointInPolygonFlat = require('point-in-polygon/flat')
var pointInPolygonNested = require('point-in-polygon/nested')
pointInPolygon(point, polygon, start=0, end=polygon.length)
Return whether point
is contained in polygon
.
point
should be a 2-item array of coordinatespolygon
should be an array of 2-item arrays of coordinates or a flat array of coordinatesstart
is an offset into polygon
. default 0
end
is an offset into polygon
. default polygon.length
The flat or nested is detected automatically. Or you can use the specific methods if you want to
skip the check.
install
npm install point-in-polygon
license
MIT